package com.itboye.mengwu.luntan.comment.adapter;

import java.util.List;

import org.w3c.dom.Text;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList.ReplyInfo.ReContent;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.luntan.comment.bean.Reply;

public class MyTieZiCommentReplyAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private OnClickListener replyToReplyListener;
	private int parentPosition = -1;
	private List<TieZiBaoHanReply.GetAList.ReplyInfo.ReContent> replyList;
	Context context;

	public MyTieZiCommentReplyAdapter(
			Context context,
			List<TieZiBaoHanReply.GetAList.ReplyInfo.ReContent> replyList,
			int parentPosition, OnClickListener replyToReplyListener) {
		this.inflater = LayoutInflater.from(context);
		this.parentPosition = parentPosition;
		this.replyToReplyListener = replyToReplyListener;
		this.replyList = replyList;
		this.context = context;
	}

	@Override
	public int getCount() {
		int count = replyList.size();
		return replyList.size();
	}

	public void clearList() {
		this.replyList.clear();
	}

	public void updateList(
			List<TieZiBaoHanReply.GetAList.ReplyInfo.ReContent> replyList) {
		this.replyList.addAll(replyList);
	}

	@Override
	public TieZiBaoHanReply.GetAList.ReplyInfo.ReContent getItem(
			int position) {
		return replyList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_comment_reply, null);
			viewHolder = new ViewHolder();
			viewHolder.tv_comment_reply_text = (TextView) convertView
					.findViewById(R.id.tv_msg_content);
			viewHolder.tv_comment_reply_writer = (TextView) convertView
					.findViewById(R.id.tv_msg_title);
			// viewHolder.tv_comment_reply_text2=(TextView)
			// convertView.findViewById(R.id.tv_comment_reply_text2);
			// viewHolder.tv_comment_reply_writer2=(TextView)
			// convertView.findViewById(R.id.tv_comment_reply_writer2);
			viewHolder.huifupinglun = (LinearLayout) convertView
					.findViewById(R.id.huifupinglun);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();

		}
		TieZiBaoHanReply.GetAList.ReplyInfo.ReContent reply = getItem(position);
		// if (!reply.getStatus().equals("-1")) {
		viewHolder.tv_comment_reply_text.setText(reply.getMessage());
		// 判断是回复楼主或者回复其他人
		viewHolder.tv_comment_reply_writer.setText(reply.getAuthor() + ":");
		viewHolder.huifupinglun.removeAllViews();
		if (reply.getComments() != null) {
			for (int i = 0; i < reply.getComments().size(); i++) {
				View view = LayoutInflater.from(context).inflate(
						R.layout.reply_view, null);
				TextView textView1 = (TextView) view
						.findViewById(R.id.author_one);
				TextView textView2 = (TextView) view
						.findViewById(R.id.author_two);
				TextView textView3 = (TextView) view.findViewById(R.id.content);
				textView1.setText(reply.getComments().get(i).getAuthor());
				textView2.setText(reply.getAuthor() + " :");
				textView3.setText(reply.getComments().get(i).getComment());
				LayoutParams params = new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				params.setMargins(5, 5, 5, 5);
				viewHolder.huifupinglun.addView(view, params);
			}

			// int start = reply.getComments().get(0).getAuthor().length();
			// String text = reply.getAuthor() + "回复" ;
			// SpannableStringBuilder ss = new SpannableStringBuilder(text);
			// // 设置指定位置文字的背景颜色（将“回复”设置成灰色）
			// ss.setSpan(new
			// ForegroundColorSpan(Color.parseColor("#5A5A5A")),
			// start, start + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			// ss.sets
		}
		// else{
		//
		// }
		// }
		// // else {
		// viewHolder.huifupinglun.setVisibility(View.GONE);
		// }
		// else {
		// int start = reply.getAuthor().length();
		// String text = reply.getAuthor() + "回复" ;
		// SpannableStringBuilder ss = new SpannableStringBuilder(text);
		// // 设置指定位置文字的背景颜色（将“回复”设置成灰色）
		// ss.setSpan(new ForegroundColorSpan(Color.parseColor("#5A5A5A")),
		// start, start + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		// viewHolder.tv_comment_reply_writer.setText(ss+reply.getComments().get(0).getComment());
		// }
		// 记录发表回复时，对应的position,用于确定所回复的对象，如果是楼中楼，还需记录父节点的position
		convertView.setTag(R.id.tag_first, parentPosition);
		convertView.setTag(R.id.tag_second, position);
		convertView.setTag(R.id.tag_third, reply.getAuthorId());
		convertView.setOnClickListener(replyToReplyListener);
		return convertView;
	}

	public class ViewHolder {
		private TextView tv_comment_reply_writer; // 评论者昵称
		private TextView tv_comment_reply_text; // 评论 内容\
		private TextView tv_comment_reply_writer2; // 评论者昵称
		private TextView tv_comment_reply_text2; // 评论 内容
		private LinearLayout huifupinglun;//
	}
}
